package com.jed.util;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;

public class StringUtil {

	/** 生成UUID */
	public static String getUUID() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}

	/** 实体类转为json */
	public static <T> net.sf.json.JSONObject entityToJson(T entity) {
		net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(entity);
		return jsonObject;
	}

	/** map转json */
	public static <T> org.json.JSONObject mapToJson(Map<String, Object> map) {
		org.json.JSONObject jsonObject2 = new org.json.JSONObject(map);
		return jsonObject2;
	}

	/** json转为map */
	public static Map<String, Object> jsonStrToMap(Object Object) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (Object != null && !"".equals(Object)) {
			try {
				org.json.JSONObject jsonObject = new org.json.JSONObject(Object.toString());
				Iterator<String> keys = jsonObject.keys();
				while (keys.hasNext()) {
					String key = (String) keys.next();
					String value = jsonObject.get(key).toString();
					map.put(key, value);
				}
			} catch (org.json.JSONException e) {
				e.printStackTrace();
			}
		}
		return map;
	}

	/**
	 * 判断对象是否为空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNotEmpty(Object str) {
		boolean flag = true;
		if (str != null && !str.equals("")) {
			if (str.toString().length() > 0) {
				flag = true;
			}
		} else {
			flag = false;
		}
		return flag;
	}
	
	
	/**
	 * 格式化数字100.00
	 * @param object
	 * @return
	 */
	public static String douToBig(Object object) {
		if (isNotEmpty(object)) {
			String str = object.toString();
			BigDecimal big = new BigDecimal(str);
			return big.setScale(2, BigDecimal.ROUND_HALF_UP).toString();
		}else {
			return "——";
		}
	}

}
